#include "oneperson.h"
#include "stydatabase.h"
#include <QLabel>

int g_index=0;

onePerson::onePerson(QWidget *parent) : QWidget(parent)
{
    friends = new QComboBox();
    top_lay = new QHBoxLayout();
    main_lay = new QVBoxLayout();
    /* 初始化控件 */
    container = new QWidget(this);
    diagram = new QCustomPlot(container);
    customPlot = diagram;
    setupBarChart();
    initFriendsBox();

    /* 设置布局 */
    top_lay->addWidget(friends);
    top_lay->addStretch();
    main_lay->addLayout(top_lay);
    main_lay->addWidget(container);
    this->setLayout(main_lay);
}

void onePerson::setupBarChart()
{
  demoName = "每日任务完成情况";
  // set dark background gradient:
  QLinearGradient gradient(0, 0, 0, 400);//黑色
  gradient.setColorAt(1, QColor(70, 70, 70));//灰色
  gradient.setColorAt(0, QColor(90, 90, 90));//淡一点的灰色
  gradient.setColorAt(0.38, QColor(105, 105, 105));//更淡的灰色
  customPlot->setBackground(QBrush(gradient));

  // create empty bar chart objects:
  overful = new QCPBars(customPlot->xAxis, customPlot->yAxis);//超额完成的部分
  balance = new QCPBars(customPlot->xAxis, customPlot->yAxis);//差额部分
  in_fact = new QCPBars(customPlot->xAxis, customPlot->yAxis);//实际完成的部分

  overful->setAntialiased(false);// gives more crisp, pixel aligned bar borders
  balance->setAntialiased(false);//提供更清晰、像素对齐的条形边框
  in_fact->setAntialiased(false);
  overful->setStackingGap(1);
  balance->setStackingGap(1);
  in_fact->setStackingGap(1);

  // set names and colors:
  in_fact->setName("目标内学习时长");
  in_fact->setPen(QPen(QColor(141, 39, 206).lighter(170)));
  in_fact->setBrush(QColor(141, 39, 206));

  balance->setName("目标学习时长差额");
  balance->setPen(QPen(QColor(250, 170, 20).lighter(150)));
  balance->setBrush(QColor(250, 170, 20));

  overful->setName("超额完成部分");
  overful->setPen(QPen(QColor(111, 9, 176).lighter(130)));
  overful->setBrush(QColor(111, 9, 176));
  // stack bars on top of each other:
  balance->moveAbove(in_fact);  //差额放到实际完成的工作量上面
  overful->moveAbove(balance);    //最上面放超额完成的部分

  // prepare x axis with country labels:
  QVector<QString> labels;
  for(int i=1;i<=31;i++){
      ticks<<i;
      labels<<QString::number(i);
  }
  QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
  textTicker->addTicks(ticks, labels);
  customPlot->xAxis->setTicker(textTicker);
  customPlot->xAxis->setTickLabelRotation(60);
  customPlot->xAxis->setSubTicks(false);
  customPlot->xAxis->setTickLength(0, 4);
  customPlot->xAxis->setRange(0, 32);
  customPlot->xAxis->setBasePen(QPen(Qt::white));
  customPlot->xAxis->setTickPen(QPen(Qt::white));
  customPlot->xAxis->grid()->setVisible(true);
  customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
  customPlot->xAxis->setTickLabelColor(Qt::white);
  customPlot->xAxis->setLabelColor(Qt::white);

  // prepare y axis:
  customPlot->yAxis->setRange(0, 600);
  customPlot->yAxis->setPadding(5); // a bit more space to the left border
  customPlot->yAxis->setLabel("每日学习时长\n单位：分钟");
  customPlot->yAxis->setBasePen(QPen(Qt::white));
  customPlot->yAxis->setTickPen(QPen(Qt::white));
  customPlot->yAxis->setSubTickPen(QPen(Qt::white));
  customPlot->yAxis->grid()->setSubGridVisible(true);
  customPlot->yAxis->setTickLabelColor(Qt::white);
  customPlot->yAxis->setLabelColor(Qt::white);
  customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
  customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));

  // Add data:
  in_fact->setData(ticks, g_data[g_index].in_factData);
  balance->setData(ticks, g_data[g_index].balanceData);
  overful->setData(ticks, g_data[g_index].overfulData);

  // setup legend:
  customPlot->legend->setVisible(true);
  customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
  customPlot->legend->setBrush(QColor(255, 255, 255, 100));
  customPlot->legend->setBorderPen(Qt::NoPen);
  QFont legendFont = font();
  legendFont.setPointSize(10);
  customPlot->legend->setFont(legendFont);
  customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

void onePerson::dataUpdate()
{
    qDebug()<<"g_index = "<<g_index;
    in_fact->setData(ticks, g_data[g_index].in_factData);
    balance->setData(ticks, g_data[g_index].balanceData);
    overful->setData(ticks, g_data[g_index].overfulData);
    customPlot->replot();
}

void onePerson::initFriendsBox()
{
    // 添加好友到下拉列表
    m_Accountlist = new QListWidget(this);
    friends->setObjectName(QStringLiteral("friendsComboBox"));
    friends->setMinimumSize(QSize(190, 40));
    friends->setFocusPolicy(Qt::WheelFocus);
    friends->setContextMenuPolicy(Qt::NoContextMenu);
    friends->setLocale(QLocale(QLocale::Chinese, QLocale::China));
    friends->setEditable(true);
    friends->setInsertPolicy(QComboBox::NoInsert);
    friends->setModel(m_Accountlist->model());
    friends->setView(m_Accountlist);
    for(int i=0;i<g_data.size();i++){
        AccountItem *account_item = new AccountItem();
        account_item->setAccountInfo(i,g_data[i].friends.UNickname,QString(":/img/img/headImage/head%1.png").arg(i),0);
        connect(account_item, SIGNAL(signalShowAccountInfo(int, QString)), this, SLOT(onShowAccountInfo(int, QString)));
        QListWidgetItem *list_item = new QListWidgetItem(m_Accountlist);
        m_Accountlist->setItemWidget(list_item, account_item);
    }
    friends->setEditText(g_data[0].friends.UNickname);
}

void onePerson::onShowAccountInfo(int index, QString accountName)
{
    if(accountName == "" ||accountName==NULL){
        return ;
    }
    friends->setEditText(accountName);
    friends->hidePopup();
    g_index = index;
    qDebug()<<"index = "<<index;
    this->friends->repaint();
    emit sigFriendChange();
    //qDebug()<<accountName<<",friend Edit text:"<<friends->currentText();
}

void onePerson::onRemoveAccount(int index)
{
    qDebug()<<"delete?";
}

